import React, { useState } from 'react'
import { isCascadeTree } from '@/pages/manager/blocks/main/table/all/utils'
import { isMatch } from '@/pages/manager/utils/utils'
import { IMcc } from '@/api/manager/type'
import { Mt } from '@/api/manager/mt'
import { MccModel } from '@/pages/manager/blocks/main/table/all/translator'
import { Meeting } from '@/api/manager/meeting'

export interface ItemData {
	mcc: IMcc
	nestingLevel: number
	mtKey: string
}
export interface SelectedModel {
	mcc: Mt
	status: {
		checked: boolean
	}
	mtKey: string
}
interface IL {
	mcc: Mt
	mtKey: string
}

export default function useGetList() {
	const [list, setList] = useState<ItemData[]>([])
	const [openMap, setOpenMap] = useState<{ [mtKey: string]: boolean }>({})

	const getListFromTableAll = (tree: MccModel<Meeting>, keywords: string) => {
		const isCascade = isCascadeTree(tree)
		let result = []
		if (isCascade) {
			let stack: MccModel<IMcc>[] = [tree]
			let newOpenMap: { [mtKey: string]: boolean } = {}
			while (stack.length !== 0) {
				const item = stack.shift()!
				let isMatchSearch = true
				//在线会议始终满足search
				if (item.mcc.isMt) {
					//终端
					isMatchSearch = isMatch(item.mcc, keywords)
				} else if (!item.mcc.status.isOnline) {
					//离线会议
					isMatchSearch = isMatch(item.mcc, keywords)
				}
				if (isMatchSearch) {
					result.push({
						mcc: item.mcc,
						nestingLevel: item.nestingLevel,
						mtKey: item.mtKey,
					})
					if (!item.mcc.isMt && item.mcc.status.isOnline) {
						if (
							(item.mtKey === '0.0.0.0_0' || item.mcc.id === '0_1') &&
							openMap[item.mtKey] === undefined
						) {
							//初始化默认展开本级和上级
							openMap[item.mtKey] = true
						}
						newOpenMap[item.mtKey] = openMap[item.mtKey] !== undefined ? openMap[item.mtKey] : false
						if (newOpenMap[item.mtKey]) {
							for (let i = item.children.length - 1; i >= 0; i--) {
								stack.unshift({
									mcc: item.children[i].mcc,
									mtKey: item.children[i].mtKey,
									nestingLevel: item.nestingLevel + 1,
									children: item.children[i].children,
								} as MccModel<IMcc>)
							}
						}
					}
				}
			}
			/**
			 * 由于推送消息都是单条单条处理，所以 tree 的更新存在先后性
			 * 离线会议和被删除的会议需要进行判断，从openMap内移除。否则再次上线可能会默认展开，导致其下级终端并没有出现
			 */
			setOpenMap(openMap)
		} else {
			let stack = tree.children
			for (let i = 0; i < stack.length; i++) {
				const item = stack[i]
				let isMatchSearch = isMatch(item.mcc, keywords)
				if (isMatchSearch) {
					result.push({
						mcc: item.mcc,
						nestingLevel: 0,
						children: [],
						mtKey: item.mtKey,
					})
				}
			}
		}
		setList(result)
	}
	const getListFromTable = <T extends IL>(list: T[], keywords: string) => {
		let result = []
		for (let i = 0; i < list.length; i++) {
			const item = list[i]
			let isMatchSearch = isMatch(item.mcc, keywords)
			if (isMatchSearch) {
				result.push({
					mcc: item.mcc,
					nestingLevel: 0,
					children: [],
					mtKey: item.mtKey,
				})
			}
		}
		setList(result)
	}
	return {
		list,
		openMap,
		setOpenMap,
		getListFromTableAll,
		getListFromTable,
	}
}
